#ifndef REQUEST_HANDLER_BASE_H
#define REQUEST_HANDLER_BASE_H

#include <QString>

#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtXml>

#include <QWidget>
#include <QVariant>

const QString kServerURL("http://123.196.117.46:8088/");
const QString kGetString("get");
const QString kFormatString("format");
const QString kMethodString("method");
const QString kLanguage("cn");
//const QString kCity("\u1753\uac4e");

class RequestHandler;
class ResultProcessor;

typedef QMap<QString, QVariant> Data;

struct RequestMethod {
    RequestMethod() {}
    RequestMethod(ResultProcessor *_processor, QString _rawRequest)
      : processor(_processor), rawRequest(_rawRequest) {}

    RequestMethod(ResultProcessor *_processor, QString _format, QString _method)
      : processor(_processor), format(_format), method(_method){}
    ResultProcessor *processor;
    QString format;
    QString method;

    QString rawRequest;
    struct param {
        param() {}
        param(QString _name, QVariant _value) : name(_name), value(_value) {}
        QString name;
        QVariant value;
    };

    QVector<param> params;
    inline void AddParam(QString name, QVariant value) {
        param p(name, value);
        params.push_back(p);
    }

    //int (*run)(RequestMethod &r, QDomElement &e);
};

class RequestHandler : public QObject
{
    Q_OBJECT
public:
    explicit RequestHandler(QNetworkAccessManager *manager);
    RequestHandler(QNetworkAccessManager *manager, RequestMethod request);

    virtual ~RequestHandler();
    void SetRequestMethod(RequestMethod request);
    void IssueRequest();
    void IssueRequest(QString request);
    void IssueRequest(RequestMethod request);

    void Cancel();
private:
    RequestHandler() {}

signals:
    void Error(QNetworkReply *reply);

protected slots:
    void ReplyFinished(QNetworkReply *reply);

protected:
    QNetworkAccessManager *manager_;

    RequestMethod request_;

    QNetworkReply *reply_;
};

#endif // REQUEST_HANDLER_BASE_H
